package com.poposchool.project.service.impl;

import com.poposchool.project.common.Constants;
import com.poposchool.project.controller.dto.PriceByMonth;
import com.poposchool.project.entity.StoreOrder;
import com.poposchool.project.exception.ServiceException;
import com.poposchool.project.mapper.StoreOrderMapper;
import com.poposchool.project.service.IStoreOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author wangjie
 * @since 2022-09-09
 */
@Service
public class StoreOrderServiceImpl extends ServiceImpl<StoreOrderMapper, StoreOrder> implements IStoreOrderService {
    @Resource
    StoreOrderMapper storeOrderMapper;


    @Override
    @Transactional
    public BigDecimal getAllPrice() {
        try {
            return storeOrderMapper.getAllPrice();
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
    }

    @Override
    @Transactional
    public BigDecimal getAllCost() {
        try {
            return storeOrderMapper.getAllCost();
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
    }

    @Override
    @Transactional
    public List<PriceByMonth> getAllPriceByMonth() {
        try {
            Calendar date = Calendar.getInstance();
            String year = String.valueOf(date.get(Calendar.YEAR));
            return storeOrderMapper.getAllPriceByMonth(year);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }

    }
}
